package com.ck.GUI;

/**
 * Created by ck on 2016/12/19.
 */

        import com.blacamdk.ChatNetModule.ClientMain;

        import java.awt.*;
        import java.awt.event.*;
        import java.io.IOException;
        import javax.imageio.ImageIO;
        import javax.swing.*;


/* 注册窗体 */
public class RegisterFrame extends JFrame {
    private static final long serialVersionUID = -768631070458723803L;
    private JPasswordField pwdFld;
    private JPasswordField pwd2Fld;
    private JTextField nickname;
    private JComboBox head;
    private JRadioButton sex0;
    private JRadioButton sex1;
    private JButton ok;
    private JButton reset;
    private JButton cancel;

    public RegisterFrame(){
        this.init();
        setVisible(true);
    }

    public void init(){
//        String imagePath = "/Chat.png";// 构造图片的路径
//        Image imageIcon = Toolkit.getDefaultToolkit().getImage(getClass().getClassLoader().getResource(imagePath));// 这种写法就可以解决导出的Jar包也能显示图标了
//        setIconImage(imageIcon);
//        //setIconImage(Toolkit.getDefaultToolkit().getImage("images/123.png") );
        String imagePath = "/Chat.png";// 构造图片的路径
        try{
            Image img= ImageIO.read(this.getClass().getResource(imagePath));
            this.setIconImage(img);
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.setTitle("注册新账号");//设置标题

        //设置默认窗体在屏幕中央
        int x = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
        int y = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
        this.setLocation((x - this.getWidth()) / 2, (y-this.getHeight())/ 2);
        this.setResizable(false);
        setSize(387,267);
        getContentPane().setLayout(null);
        setResizable(false);

        JLabel label = new JLabel("*昵称:"); //label显示
        label.setBounds(24, 36, 59, 17);
        getContentPane().add(label);

        nickname = new JTextField(); //昵称
        nickname.setBounds(90, 34, 110, 22);
        getContentPane().add(nickname);

        JLabel label5 = new JLabel("*密码:");
        label5.setBounds(24, 72, 50, 17);
        getContentPane().add(label5);

        JLabel label3 = new JLabel("*确认密码:");
        label3.setBounds(20, 107, 70, 17);
        getContentPane().add(label3);

        pwdFld = new JPasswordField();//密码框
        pwdFld.setBounds(90, 70, 110, 22);
        getContentPane().add(pwdFld);

        pwd2Fld = new JPasswordField();
        pwd2Fld.setBounds(90, 105, 110, 22);
        getContentPane().add(pwd2Fld);

        JLabel label4 = new JLabel("性别:");
        label4.setBounds(210, 36, 50, 17);
        getContentPane().add(label4);

        sex1 = new JRadioButton("男",true);
        sex1.setBounds (268, 31,50, 20);
        getContentPane().add(sex1);
        sex0 = new JRadioButton("女");
        sex0.setBounds(320, 31, 50, 20);
        getContentPane().add(sex0);
        ButtonGroup buttonGroup = new ButtonGroup();//单选按钮组
        buttonGroup.add(sex0);
        buttonGroup.add(sex1);

//        JLabel label6 = new JLabel("头像:");
//        label6.setBounds(210, 72, 50, 17);
//        getContentPane().add(label6);
//
//        head = new JComboBox();//下拉列表图标
//        head.setBounds(278, 70, 70, 45);
//        head.setMaximumRowCount(5);
//        for (int i = 0; i < 11; i++) {
//            head.addItem(new ImageIcon("images/" + i + ".png"));
//            //通过循环添加图片，注意图片名字要取成1,2,3,4,5,等
//        }
//        head.setSelectedIndex(0);
//        getContentPane().add(head);

        //按钮
        ok = new JButton("确认");
        ok.setBounds(27, 176, 60, 28);
        getContentPane().add(ok);

        reset = new JButton("重填");
        reset.setBounds(123, 176, 60, 28);
        getContentPane().add(reset);

        cancel = new JButton("取消");
        cancel.setBounds(268, 176, 60, 28);
        getContentPane().add(cancel);

        //////////////////////注册事件监听器////////////////////////
        //取消按钮监听事件处理
        cancel.addActionListener(new ActionListener() {
            public void actionPerformed(final ActionEvent event) {
                RegisterFrame.this.dispose();
            }
        });
        //关闭窗口
        this.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e) {
                RegisterFrame.this.dispose();
            }
        });

        // 重置按钮监听事件处理
        reset.addActionListener(e -> {
            nickname.setText("");
            pwdFld.setText("");
            pwd2Fld.setText("");
            nickname.requestFocusInWindow();//用户名获得焦点
        });

        //确认按钮监听事件处理
        ok.addActionListener(e -> {
            if (pwdFld.getPassword().length==0 || pwd2Fld.getPassword().length==0) {
                JOptionPane.showMessageDialog(RegisterFrame.this, "带 “ * ” 为必填内容!");
                //判断用户名和密码是否为空
            } else if (!new String(pwdFld.getPassword()).equals(new String(pwd2Fld.getPassword()))) {
                JOptionPane.showMessageDialog(RegisterFrame.this, "两次输入密码不一致!");
                pwdFld.setText("");
                pwd2Fld.setText("");
                pwdFld.requestFocusInWindow();
                //判断两次密码是否一致
            }else
            {
                String ret = ClientMain.register(nickname.getText(), new String(pwdFld.getPassword()));
                if(ret.equals("成功"))
                {
                    //成功关闭窗口
                    JOptionPane.showMessageDialog(RegisterFrame.this, ret+"");
                    RegisterFrame.this.dispose();
                }
                else
                {
                    //提示ret
                    JOptionPane.showMessageDialog(RegisterFrame.this, ret+"");
                    reset.doClick();
                }
            }
        });
    }

}